package com.t235.gc.device.service;

import com.t235.gc.AjaxResult;
import com.t235.gc.devicevo.AddDeviceVo;
import com.t235.gc.devicevo.IndexHeadVo;
import com.t235.gc.entity.*;
import com.t235.gc.mapper.*;
import com.t235.gc.devicevo.DeviceVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description :
 * Created by Li on 2021-03-23 09:23:08
 */
@Service
public class DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceCabinetMapper deviceCabinetMapper;
    @Autowired
    private DeviceVersionMapper deviceVersionMapper;
    @Autowired
    private VenuesMapper venuesMapper;
    @Autowired
    private SysManagerMapper sysManagerMapper;
    @Autowired
    private PartnersMapper partnersMapper;
    public List<DeviceVo> findByAll(DeviceVo deviceVo) {
        List<DeviceVo> deviceVoList = deviceMapper.findByAll(deviceVo);
        /*for (DeviceVo device : deviceVoList) {
            device.setDz(device.getProvince() + device.getCity() + device.getArea() + device.getAddress());
            String venuesname = venuesMapper.selectByPrimaryKey(device.getVenuesid()).getVenuesname();
            device.setVenuesname(venuesname != null ? venuesname : "无");
            Partners partners = partnersMapper.selectByPrimaryKey(device.getPartnersid());
            device.setPartnersName(partners != null ? partners.getUsername() : "无");
            SysManager sysManager = sysManagerMapper.selectByPrimaryKey(device.getSortingid());
            device.setSortingname(sysManager != null ? sysManager.getUsername() : "无");
            SysManager sysManager1 = sysManagerMapper.selectByPrimaryKey(device.getOperatorsid());
            device.setOperatorsname(sysManager1 != null ? sysManager1.getUsername() : "无");
        }*/
        return deviceVoList;
    }

    public IndexHeadVo findIndexHeadNumber() {
        return deviceMapper.findIndexHeadNumber();
    }

    public Map<String, Object> getDeviceCriteriaQuerySelect() {
        Map<String, Object> selectMap = new HashMap<>();
        List<Venues> venuesList = venuesMapper.selectAll();
        selectMap.put("venuesList", venuesList);
        List<SysManager> sortingList = sysManagerMapper.selectByManagerType(3);
        selectMap.put("sortingList", sortingList);
        List<SysManager> operatorsList = sysManagerMapper.selectByManagerType(4);
        selectMap.put("operatorsList", operatorsList);
        List<Partners> partnersList = partnersMapper.selectAll();
        selectMap.put("partnersList", partnersList);
        return selectMap;
    }

    public SysManager gitRatioByManagerId(String managerId) {
        return sysManagerMapper.selectByPrimaryKey(managerId);
    }
    public Venues gitAddressByVenuesName(String venuesName) {
        return venuesMapper.selectByVenuesName(venuesName);
    }

    public boolean addDevice(AddDeviceVo addDeviceVo) {
        Device device = new Device();
        DeviceCabinet deviceCabinet = new DeviceCabinet();
        BeanUtils.copyProperties(addDeviceVo, device);
        BeanUtils.copyProperties(addDeviceVo, deviceCabinet);
        int insert = deviceMapper.insertSelective(device);
        int insert1 = deviceCabinetMapper.insertSelective(deviceCabinet);
        if(insert+insert1 <1) {
            return false;
        }
        return true;
    }

    public boolean updDevice(AddDeviceVo addDeviceVo) {
        Device device = new Device();
        DeviceCabinet deviceCabinet = new DeviceCabinet();
        BeanUtils.copyProperties(addDeviceVo, device);
        BeanUtils.copyProperties(addDeviceVo, deviceCabinet);
        int update = deviceMapper.updateByPrimaryKeySelective(device);
        int update1 = deviceCabinetMapper.updateByDeviceIdSelective(deviceCabinet);
        if(update+update1 <1) {
            return false;
        }
        return true;
    }

    public boolean updDeviceVersion(Long deviceid, String name) {
        DeviceVersion deviceVersion = new DeviceVersion();
        deviceVersion.setName(name);
        int i = deviceVersionMapper.insertSelective(deviceVersion);
        if(i<0){
            return false;
        }
        DeviceVersion deviceVersion1 =deviceVersionMapper.selectIdByName(name);
        Device device = new Device();
        device.setDeviceid(deviceid);
        device.setDeviceversion(deviceVersion1.getVersionid() + "");
        int update = deviceMapper.updateDeviceVersion(device);
        if (update > 0) {
            return true;
        }
        return false;
    }

    public Integer delDevice(Long deviceId) {
        try {
            deviceMapper.deleteByPrimaryKey(deviceId);
            deviceCabinetMapper.deleteByDeviceId(deviceId);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    public List<AddDeviceVo> selectByDeviceId(String deviceId) {
        return deviceMapper.selectByDeviceId(deviceId);
    }
}
